create
create
This method creates a new appointment rating, validates the input, prevents duplicates, and optionally auto-approves or rejects based on rating criteria.
-
Extract Input Data
- Get the fields from
createRatingDto:customer_id,booking_id,overall_rating,provider_rating,staff_rating,clinic_rating, andcomments. - Determine if the appointment is internal by checking if
booking_idstarts with'BKD'.
- Get the fields from
-
Check for Existing Rating
- Query the
rating_appointmenttable to check if a rating already exists for thebooking_id. - If found, throw a
ConflictExceptionindicating the appointment has already been rated.
- Query the
-
Validate Customer Exists
- Check if a customer with
customer_idexists. - If not, throw a
NotFoundExceptionfor invalid request data.
- Check if a customer with
-
Fetch Appointment Data
- If internal appointment, query the
appointmentstable usingbooking_id. - Else, query the
appointments_ehrtable using the numericbooking_idonemt_appointment_idfield. - Select related
customer,agent, andbusinessLocationdetails. - If appointment not found, throw a
NotFoundException.
- If internal appointment, query the
-
Verify Customer Matches Appointment
- Confirm the
customer.idfrom appointment matches the inputcustomer_id. - If mismatch, throw a
BadRequestException.
- Confirm the
-
Create Rating Entity
- Use repository’s
createmethod to prepare the rating with provided scores, comments, status set to'pending', and the current date/time.
- Use repository’s
-
Save Rating
- Persist the rating entity to the database.
- If saving fails, throw a
RatingCreationFailedException.
-
Update Rating Notification Status
- Update the rating notification record linked to the appointment to status
RATED. - Log an error if this update fails, but continue execution.
- Update the rating notification record linked to the appointment to status
-
Auto-Approval / Auto-Rejection Logic
-
If no comments provided AND
provider_rating>= 4:- Fetch the first agent with agent type
AgentAdminto act as approving agent. - If none found, log an error.
- Otherwise, check if the customer's email ends with
'@alethian.com'(indicating internal account). - If internal and environment is production, auto-reject rating; otherwise, auto-approve.
- Call
approveAppointmentRatingwith this agent and status.
- Fetch the first agent with agent type
-
-
Return Success Response
- Return a success message confirming the rating submission.